//
//  KSPickingGoodsTableViewCell.swift
//  Supermarket
//
//  Created by liuxm on 2017/9/30.
//  Copyright © 2017年 kushao. All rights reserved.
//

import UIKit

protocol KSChooseGoodsNumberDelegate: NSObjectProtocol {
    func chooseGoodsNumber(num: String,row: NSInteger)
    func hintMessage(msg: String)
}

/// 分拣、拣货
class KSPickingGoodsTableViewCell: UITableViewCell,UITextFieldDelegate {
    
    @IBOutlet weak var goodsImage: UIImageView!
    @IBOutlet weak var goodsNameLabel: UILabel!
    @IBOutlet weak var skuDetailLabel: UILabel!
    @IBOutlet weak var shelfLabel: UILabel!
    @IBOutlet weak var numLabel: UILabel!
    @IBOutlet weak var numberTxtField: UITextField!
    private var num = 0
    weak var delegate: KSChooseGoodsNumberDelegate?
    private var selectRow = 0
    private var tmpModel: KSStorageFjDetailListGoodsModel?
    override func awakeFromNib() {
        super.awakeFromNib()
        numberTxtField.delegate = self
    }
    
    func setDataWithModel(model: KSStorageFjDetailListGoodsModel,row: NSInteger) {
        goodsImage.sd_setImage(with: URL.init(string: model.goods_image!), placeholderImage: UIImage.init(named: "Common_GoodsPlace"))
        goodsNameLabel.text = model.goods_name!
        numLabel.text = "需求数：" + "\(model.goods_num)"
        shelfLabel.text = "货架位置：" + model.shelf!
        self.selectRow = row
        self.tmpModel = model
    }
    
    @IBAction func subtractEvent(_ sender: UITextField) {
        num = num - 1
        if num < 0 {
            num = 0
            self.delegate?.hintMessage(msg: "拣货数不能小于0")
        }
        self.numberTxtField.text = "\(num)"
    }
    
    @IBAction func addEvent(_ sender: UITextField) {
        num = num + 1
        if num > (tmpModel?.goods_num)! {
            num = num - 1
            self.delegate?.hintMessage(msg: "拣货数不能大于需求数")
        }
        self.numberTxtField.text = "\(num)"
        
    }
    
    @IBAction func textFieldChange(_ sender: UITextField) {
        num = Int(sender.text!)!
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        self.delegate?.chooseGoodsNumber(num: textField.text!, row: selectRow)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.numberTxtField.resignFirstResponder()
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
